package com.buka.recipe.system.service.impl;

import com.buka.recipe.system.entity.Department;
import com.buka.recipe.system.entity.User;
import com.buka.recipe.system.repository.UserRepository;
import com.buka.recipe.system.service.DepartmentService;
import com.buka.recipe.system.service.UserService;
import jakarta.persistence.criteria.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final DepartmentService departmentService;
    private final UserRepository userRepository;

    @Autowired
    public UserServiceImpl(DepartmentService departmentService, UserRepository userRepository) {
        this.departmentService = departmentService;
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return user;
    }

    @Override
    public void save(User user) {
        userRepository.save(user);
    }

    @Override
    public void delete(User user) {
        userRepository.delete(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public User getUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    public User getUserByOpenid(String openId) {
        return userRepository.findByOpenid(openId);
    }

    @Override
    public Page<User> getAll(String username, String name, String phone, Boolean enabled, Integer current, Integer pageSize) {
        // 得到当前用户能操作的部门
        List<Department> departments = departmentService.getAll();
        Sort sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "id"));
        Pageable pageable = PageRequest.of(current, pageSize, sort);
        Specification<User> specification = (root, query, builder) -> {
            List<Predicate> list = new ArrayList<>();
            if (StringUtils.hasLength(username)) {
                list.add(builder.like(root.get("username"), "%" + username + "%"));
            }
            if (StringUtils.hasLength(name)) {
                list.add(builder.like(root.get("name"), "%" + name + "%"));
            }
            if (StringUtils.hasLength(phone)) {
                list.add(builder.like(root.get("phone"), "%" + phone + "%"));
            }
            if (enabled != null) {
                list.add(builder.equal(root.get("enabled"), enabled));
            }
            list.add(root.get("department").in(departments));
            Predicate[] predicates = new Predicate[list.size()];
            return builder.and(list.toArray(predicates));
        };
        return userRepository.findAll(specification, pageable);
    }
}
